package com.qigao.mall.channel.weichat;

import com.alibaba.fastjson.JSONObject;
import com.qigao.mall.channel.ChannelCore;
import com.qigao.mall.commons.dto.MerChannelDto;
import com.qigao.mall.commons.enums.ChannelActionEnum;
import com.qigao.mall.commons.enums.ChannelCodeEnum;
import com.qigao.mall.commons.enums.ChannelInfoEnum;
import com.qigao.mall.commons.enums.PayProductEnum;
import com.qigao.mall.commons.param.ChannelReqParam;
import com.qigao.mall.commons.param.ChannelRespParam;
import com.qigao.mall.commons.utils.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/***
 * 添加分账接收方
 */
@Service
public class ProfitSharingAddreceiver extends AbstractWeichatTrade implements ChannelCore {

    private static Logger logger= LoggerFactory.getLogger(ProfitSharingAddreceiver.class);


    @Override
    public boolean isSupport(PayProductEnum payProductEnum, ChannelActionEnum channelActionEnum, ChannelInfoEnum channelInfoEnum) {
        return ChannelActionEnum.ORDER_DIVIDE.equals(channelActionEnum) && PayProductEnum.WEICHAT_PROFIT_ADD.equals(payProductEnum);
    }


    @Override
    protected String getSelfReqUrl() {
        return channelConstant.getProfitAddUrl();
    }

    public ChannelRespParam channelProcess(MerChannelDto merChannelDto, ChannelReqParam channelReqParam) {
        //1.构造请求参数
        String reqContent=buildReqParam(merChannelDto,channelReqParam);
        //2.发送请求
        String reqUrl=getSelfReqUrl()==null?channelConstant.getWeichatOrderUrl():getSelfReqUrl();
        logger.info("[微信交易]请求内容:{}",reqContent);
        String respContent= HttpClientUtil.postXmlWithKey(reqUrl,reqContent,merChannelDto.getResvKey(),merChannelDto.getChannelPartenerNo());
        logger.info("[微信交易]返回内容:{}",respContent);
        //3.处理响应
        ChannelRespParam channelRespParam=this.convertRespData(respContent,merChannelDto);
        logger.info("[微信交易]返回转换后内容:{}",channelRespParam);
        //3.处理响应
        return channelRespParam;
    }

    @Override
    public Map buildSelfReqParam(MerChannelDto merChannelDto, ChannelReqParam channelReqParam) {
        Map reqParam=new ConcurrentHashMap<String,String>();
        JSONObject reqContent= JSONObject.parseObject(channelReqParam.getReqContent());
        JSONObject receiveJsonObj=new JSONObject();
        receiveJsonObj.put("type","PERSONAL_OPENID");
        receiveJsonObj.put("account",reqContent.getString("recvUserNo"));
        receiveJsonObj.put("relation_type","USER");
        reqParam.put("receiver",receiveJsonObj.toString());
        return reqParam;
    }

    @Override
    protected ChannelRespParam buildSelfRespData(Map respMap, MerChannelDto merChannelDto) {
        String receiver= (String) respMap.get("receiver");
        Map respDataMap=new HashMap();
        respDataMap.put("receiver",receiver);
        String retStr=JSONObject.toJSONString(respDataMap);
        logger.info("[增加分账接收方]返回给数据:{}",retStr);
        ChannelRespParam channelRespParam=new ChannelRespParam();
        channelRespParam.setRespCode(ChannelCodeEnum.SUCCESS.getCode());
        channelRespParam.setRespMsg(ChannelCodeEnum.SUCCESS.getName());
        channelRespParam.setRespContent(retStr);
        return  channelRespParam;
    }



}
